﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKGeometryFuncs;
using YJKRevitDimensionText.DimCreator;
using YJKRevitDimensionText.OneKeyPlanDim.DimesionDataEx;

namespace YJKRevitDimensionText.OneKeyPlanDim.OutWallThreeDim
{
	public class WallOperattionUtil
	{
		public static void GetWallWidthLocationCurve(Wall wall, out Curve outCurve, out Curve innerCurve)
		{
			outCurve = null;
			innerCurve = null;
			Curve curve = (wall.Location as LocationCurve).Curve;
			if (null != curve)
			{
				XYZ xyz = wall.Orientation.Normalize().Multiply(wall.Width / 2.0);
				outCurve = curve.CreateTransformed(Transform.CreateTranslation(xyz));
				innerCurve = curve.CreateTransformed(Transform.CreateTranslation(-xyz));
			}
		}

		public static XYZ GetWallLocationCenter(Wall wall)
		{
			Curve curve = (wall.Location as LocationCurve).Curve;
			XYZ endPoint = curve.GetEndPoint(0);
			XYZ endPoint2 = curve.GetEndPoint(1);
			return Geometry.CalculatMidPoint(endPoint, endPoint2);
		}

		public static XYZ GetOutWallThickDimPoint(Wall wall, List<WallDataEx> lstWallEx)
		{
			try
			{
				foreach (WallDataEx wallDataEx in lstWallEx)
				{
					if (wallDataEx.WallId.IntegerValue == wall.Id.IntegerValue)
					{
						XYZ endPoint = wallDataEx.ProfileLine.GetEndPoint(0);
						XYZ endPoint2 = wallDataEx.ProfileLine.GetEndPoint(1);
						return Geometry.CalculatMidPoint(endPoint, endPoint2);
					}
				}
			}
			catch (Exception)
			{
				return null;
			}
			return null;
		}

		public static XYZ GetWallLocVector(Wall wall)
		{
			Curve curve = (wall.Location as LocationCurve).Curve;
			XYZ endPoint = curve.GetEndPoint(0);
			return (curve.GetEndPoint(1) - endPoint).Normalize();
		}

		public static void GetWallStartEndPoint(Wall wall, out XYZ ptS, out XYZ ptE)
		{
			ptS = null;
			ptE = null;
			Curve curve = (wall.Location as LocationCurve).Curve;
			ptS = curve.GetEndPoint(0);
			ptE = curve.GetEndPoint(1);
		}

		public static List<FamilyInstance> GetWallDoorWindow(Wall wall)
		{
			List<FamilyInstance> list = new List<FamilyInstance>();
			foreach (ElementId elementId in wall.FindInserts(true, true, true, true))
			{
				Element element = wall.Document.GetElement(elementId);
				if (element != null && (element.Category.Id.IntegerValue == -2000014 || element.Category.Id.IntegerValue == -2000023))
				{
					list.Add(element as FamilyInstance);
				}
			}
			return list;
		}

		public static double GetTwoWallDistance(Wall wallF, Wall wallS)
		{
			Line line = (wallF.Location as LocationCurve).Curve as Line;
			Line line2 = (wallS.Location as LocationCurve).Curve as Line;
			if (null != line && null != line2)
			{
				return WallOperattionUtil.GetParallelLineDis(line, line2);
			}
			return 0.0;
		}

		private static double GetParallelLineDis(Line lineF, Line lineS)
		{
			double result;
			try
			{
				XYZ endPoint = lineF.GetEndPoint(0);
				XYZ endPoint2 = lineF.GetEndPoint(1);
				XYZ endPoint3 = lineS.GetEndPoint(0);
				XYZ endPoint4 = lineS.GetEndPoint(1);
				XYZ xyz = Geometry.CalculatMidPoint(endPoint, endPoint2);
				double num = endPoint3.X - endPoint4.X;
				if (Math.Abs(num - 0.0) < 1E-07)
				{
					result = Math.Abs(xyz.X - endPoint3.X);
				}
				else
				{
					double num2 = (endPoint3.Y - endPoint4.Y) / num;
					double num3 = endPoint3.Y - endPoint3.X * num2;
					result = Math.Abs(num2 * xyz.X - xyz.Y + num3) / Math.Sqrt(num2 * num2 + 1.0);
				}
			}
			catch (Exception)
			{
				result = 0.0;
			}
			return result;
		}

		public static Reference GetWallProfileRef(UIApplication uiApp, Wall wall)
		{
			foreach (object obj in ElementAssistFunc.GetElementFaces(uiApp, uiApp.ActiveUIDocument.Document, wall))
			{
				PlanarFace planarFace = obj as PlanarFace;
				if (!(null == planarFace) && planarFace.FaceNormal.Normalize().IsAlmostEqualTo(wall.Orientation.Normalize()))
				{
					if (wall.Document.IsLinked)
					{
						return WallOperattionUtil.GetWallPlanarFaceReference(uiApp, planarFace, wall);
					}
					return planarFace.Reference;
				}
			}
			return null;
		}

		private static Reference GetWallPlanarFaceReference(UIApplication uiApp, PlanarFace face, Wall wall)
		{
			XYZ xyz = new XYZ(face.Origin.X, face.Origin.Y, uiApp.ActiveUIDocument.ActiveView.GenLevel.ProjectElevation);
			XYZ xyz2 = xyz - WallOperattionUtil.GetWallLocVector(wall).Multiply(AssistFunc.mmToFeet(1.0));
			Line line = Line.CreateBound(xyz, xyz2);
			return uiApp.ActiveUIDocument.Document.Create.NewModelCurve(line, uiApp.ActiveUIDocument.Document.ActiveView.SketchPlane).GeometryCurve.Reference;
		}

		public static bool GetOutWallThicknessRef(List<WallDataEx> lstOutWallEx, UIApplication uiApp, Wall wall, Line crossLine, ref ReferenceInfo outRefInfo, ref ReferenceInfo innerRefInfo)
		{
			try
			{
				Line line = null;
				Line line2 = null;
				foreach (WallDataEx wallDataEx in lstOutWallEx)
				{
					if (wallDataEx.WallId.IntegerValue == wall.Id.IntegerValue)
					{
						line = (wallDataEx.InLine as Line);
						line2 = (wallDataEx.OutLine as Line);
						break;
					}
				}
				if (null == line || null == line2)
				{
					return false;
				}
				XYZ wallLocVector = WallOperattionUtil.GetWallLocVector(wall);
				XYZ xyzpoint = crossLine.Project(line.GetEndPoint(0)).XYZPoint;
				XYZ xyz = xyzpoint + wallLocVector.Normalize().Multiply(0.0032808398950131233);
				XYZ xyzpoint2 = crossLine.Project(line2.GetEndPoint(0)).XYZPoint;
				XYZ xyz2 = xyzpoint2 + wallLocVector.Normalize().Multiply(0.0032808398950131233);
				Line line3 = Line.CreateBound(xyzpoint, xyz);
				Line line4 = Line.CreateBound(xyzpoint2, xyz2);
				XYZ position = Geometry.CalculateFootPoint(crossLine, line.GetEndPoint(0));
				XYZ position2 = Geometry.CalculateFootPoint(crossLine, line2.GetEndPoint(0));
				Reference reference = uiApp.ActiveUIDocument.Document.Create.NewDetailCurve(uiApp.ActiveUIDocument.ActiveView, line3).GeometryCurve.Reference;
				Reference reference2 = uiApp.ActiveUIDocument.Document.Create.NewDetailCurve(uiApp.ActiveUIDocument.ActiveView, line4).GeometryCurve.Reference;
				innerRefInfo = new ReferenceInfo(reference, position);
				outRefInfo = new ReferenceInfo(reference2, position2);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static bool GetOutWallThicknessRefDic(UIApplication uiApp, Wall wall, ref Dictionary<XYZ, Reference> dicRef)
		{
			try
			{
				FaceArray elementFaces = ElementAssistFunc.GetElementFaces(uiApp, uiApp.ActiveUIDocument.Document, wall);
				XYZ xyz = wall.Orientation.Normalize();
				foreach (object obj in elementFaces)
				{
					PlanarFace planarFace = obj as PlanarFace;
					if (planarFace.FaceNormal.Normalize().IsAlmostEqualTo(xyz))
					{
						if (wall.Document.IsLinked)
						{
							Reference wallPlanarFaceReference = WallOperattionUtil.GetWallPlanarFaceReference(uiApp, planarFace, wall);
							dicRef.Add(planarFace.Origin, wallPlanarFaceReference);
						}
						else
						{
							dicRef.Add(planarFace.Origin, planarFace.Reference);
						}
					}
					else if (planarFace.FaceNormal.Normalize().IsAlmostEqualTo(-xyz))
					{
						if (wall.Document.IsLinked)
						{
							Reference wallPlanarFaceReference2 = WallOperattionUtil.GetWallPlanarFaceReference(uiApp, planarFace, wall);
							dicRef.Add(planarFace.Origin, wallPlanarFaceReference2);
						}
						else
						{
							dicRef.Add(planarFace.Origin, planarFace.Reference);
						}
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static bool WallHasWinOrDoor(Wall wall)
		{
			foreach (ElementId elementId in wall.FindInserts(true, true, true, true))
			{
				Element element = wall.Document.GetElement(elementId);
				if (element != null && (element.Category.Id.IntegerValue == -2000014 || element.Category.Id.IntegerValue == -2000023))
				{
					return true;
				}
			}
			return false;
		}

		public static Curve GetTwoParallelWallCurve(Wall wallF, Wall wallS)
		{
			XYZ wallLocationCenter = WallOperattionUtil.GetWallLocationCenter(wallF);
			Curve curve = (wallS.Location as LocationCurve).Curve.Clone();
			curve.MakeUnbound();
			XYZ xyzpoint = curve.Project(wallLocationCenter).XYZPoint;
			return Line.CreateBound(wallLocationCenter, xyzpoint);
		}

		public static Curve GetPointToWallCurve(XYZ pt, Wall wall)
		{
			Curve zeroHeightCurve = OutWallThreeDimManager.GetZeroHeightCurve((wall.Location as LocationCurve).Curve.Clone());
			zeroHeightCurve.MakeUnbound();
			XYZ xyz = new XYZ(pt.X, pt.Y, 0.0);
			XYZ xyzpoint = zeroHeightCurve.Project(xyz).XYZPoint;
			return Line.CreateBound(xyz, xyzpoint);
		}

		public static double GetTwoParallelWallMinDis(Wall wallF, Wall wallS)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			WallOperattionUtil.GetWallStartEndPoint(wallF, out xyz, out xyz2);
			XYZ xyz3 = null;
			XYZ xyz4 = null;
			WallOperattionUtil.GetWallStartEndPoint(wallS, out xyz3, out xyz4);
			if (xyz == null || xyz2 == null || xyz3 == null || xyz4 == null)
			{
				return 10.0;
			}
			Curve curve = (wallS.Location as LocationCurve).Curve;
			curve.MakeUnbound();
			XYZ xyzpoint = curve.Project(xyz).XYZPoint;
			XYZ xyzpoint2 = curve.Project(xyz2).XYZPoint;
			Curve curve2 = Line.CreateBound(xyz3, xyz4);
			Line line = Line.CreateBound(xyzpoint, xyzpoint2);
            if ((int)curve2.Intersect(line) == 64)
			{
				return 3.2808398950131235;
			}
			double num = xyz3.DistanceTo(xyzpoint);
			double num2 = xyz3.DistanceTo(xyzpoint2);
			double num3 = num2;
			if (num < num2)
			{
				num3 = num;
			}
			double num4 = xyz4.DistanceTo(xyzpoint);
			double num5 = xyz4.DistanceTo(xyzpoint2);
			double num6 = num4;
			if (num5 < num4)
			{
				num6 = num5;
			}
			if (num3 >= num6)
			{
				return num6;
			}
			return num3;
		}

		public static double GetWallBaoWenLayerThick(Wall wall)
		{
			double result = 0.0;
			try
			{
				foreach (CompoundStructureLayer compoundStructureLayer in wall.WallType.GetCompoundStructure().GetLayers())
				{
					if ((int)compoundStructureLayer.Function == 3)
					{
						result = compoundStructureLayer.Width;
						break;
					}
				}
			}
			catch (Exception)
			{
				return result;
			}
			return result;
		}

		public static List<Wall> GetVertWall(Wall wall, List<Wall> lstWall)
		{
			List<Wall> list = new List<Wall>();
			XYZ xyz = null;
			XYZ xyz2 = null;
			WallOperattionUtil.GetWallStartEndPoint(wall, out xyz, out xyz2);
			if (xyz != null && xyz2 != null)
			{
				Curve curve = (wall.Location as LocationCurve).Curve;
				foreach (Wall wall2 in lstWall)
				{
					Curve curve2 = (wall2.Location as LocationCurve).Curve;
					IntersectionResultArray intersectionResultArray = null;
					curve.Intersect(curve2, out intersectionResultArray);
					if (intersectionResultArray != null && intersectionResultArray.Size == 1)
					{
						XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
						double num = xyz.DistanceTo(xyzpoint);
						double num2 = xyz2.DistanceTo(xyzpoint);
						if (Geometry.IsEqual(num, 0.0) || Geometry.IsEqual(num2, 0.0))
						{
							list.Add(wall2);
						}
					}
				}
			}
			return list;
		}

		public static List<Wall> GetVertWall(List<Wall> lstSrcWall, List<Wall> lstWall)
		{
			List<Wall> list = new List<Wall>();
			foreach (Wall wall in lstSrcWall)
			{
				List<Wall> vertWall = WallOperattionUtil.GetVertWall(wall, lstWall);
				if (vertWall.Count > 0)
				{
					foreach (Wall wall2 in vertWall)
					{
						if (!WallOperattionUtil.HasElement(list.Cast<Element>().ToList<Element>(), wall2))
						{
							list.Add(wall2);
						}
					}
				}
			}
			return list;
		}

		public static bool HasElement(List<Element> lstElem, Element elem)
		{
			using (List<Element>.Enumerator enumerator = lstElem.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Id.IntegerValue == elem.Id.IntegerValue)
					{
						return true;
					}
				}
			}
			return false;
		}

		public static bool IsSameLineGrid(Wall wall, Grid grid)
		{
			try
			{
				Line line = (wall.Location as LocationCurve).Curve as Line;
				Line line2 = grid.Curve as Line;
				if (null != line && null != line2 && Geometry.IsParallel(line.Direction, line2.Direction) && Geometry.IsEqual(WallOperattionUtil.GetParallelLineDis(line, line2), 0.0))
				{
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		public static void GetLstWallMaxPoint(List<Wall> lstWall, out XYZ ptMaxS, out XYZ ptMaxE)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ xyz = null;
			XYZ xyz2 = null;
			WallOperattionUtil.GetWallStartEndPoint(lstWall.First<Wall>(), out xyz, out xyz2);
			Line line = null;
			if (xyz != null && xyz2 != null)
			{
				line = Line.CreateBound(xyz, xyz2);
			}
			line.MakeUnbound();
			Dictionary<XYZ, XYZ> dictionary = new Dictionary<XYZ, XYZ>();
			foreach (Wall wall in lstWall)
			{
				XYZ xyz3 = null;
				XYZ xyz4 = null;
				WallOperattionUtil.GetWallStartEndPoint(wall, out xyz3, out xyz4);
				if (xyz3 != null && xyz4 != null)
				{
					XYZ xyzpoint = line.Project(xyz3).XYZPoint;
					list.Add(xyzpoint);
					dictionary.Add(xyzpoint, xyz3);
					xyzpoint = line.Project(xyz4).XYZPoint;
					list.Add(xyzpoint);
					dictionary.Add(xyzpoint, xyz4);
				}
			}
			List<XYZ> source = WallOperattionUtil.SortPointsWithVector(list, line.Direction.Normalize());
			ptMaxS = source.First<XYZ>();
			ptMaxE = source.Last<XYZ>();
		}

		public static List<XYZ> SortPointsWithVector(List<XYZ> srcPoints, XYZ vecDirection)
		{
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			list.AddRange(srcPoints);
			SortedList<double, List<XYZ>> sortedList = new SortedList<double, List<XYZ>>();
			XYZ xyz = vecDirection.Multiply(1.0);
			foreach (XYZ xyz2 in srcPoints)
			{
				double d = xyz.AngleTo(xyz2);
				double key = Math.Sqrt(xyz2.X * xyz2.X + xyz2.Y * xyz2.Y + xyz2.Z * xyz2.Z) * Math.Cos(d);
				if (!sortedList.ContainsKey(key))
				{
					sortedList.Add(key, new List<XYZ>
					{
						xyz2
					});
				}
				else
				{
					sortedList[key].Add(xyz2);
				}
			}
			if (sortedList.Count > 0)
			{
				IEnumerator<KeyValuePair<double, List<XYZ>>> enumerator2 = sortedList.GetEnumerator();
				while (enumerator2.MoveNext())
				{
					List<XYZ> list3 = list2;
					KeyValuePair<double, List<XYZ>> keyValuePair = enumerator2.Current;
					list3.AddRange(keyValuePair.Value);
				}
				if (list2.Count == srcPoints.Count)
				{
					list = list2;
				}
			}
			return list;
		}

		public static List<WallDataMerge> GetMergeWallData(UIApplication uiApp, BaseDataOper baseData)
		{
			List<WallDataMerge> list = new List<WallDataMerge>();
			List<WallDataEx> outWallData = baseData.GetOutWallData();
			List<WallDataEx> list2 = new List<WallDataEx>();
			foreach (WallDataEx wallDataEx in outWallData)
			{
				if (!WallOperattionUtil.HasWallInLst(uiApp, list2, wallDataEx))
				{
					Curve curve = wallDataEx.OutLine;
					List<WallDataEx> list3 = new List<WallDataEx>();
					list3.Add(wallDataEx);
					list2.Add(wallDataEx);
					foreach (WallDataEx wallDataEx2 in outWallData)
					{
						if (!WallOperattionUtil.HasWallInLst(uiApp, list2, wallDataEx2))
						{
							Curve curve2 = CurveAssistFunc.Combine2Curve(uiApp, curve, wallDataEx2.OutLine);
							double num = 0.0;
							bool wallDataWidth = WallOperattionUtil.GetWallDataWidth(uiApp.ActiveUIDocument.Document, wallDataEx, out num);
							double num2 = 0.0;
							bool wallDataWidth2 = WallOperattionUtil.GetWallDataWidth(uiApp.ActiveUIDocument.Document, wallDataEx2, out num2);
							if (null != curve2 && wallDataWidth && wallDataWidth2 && Geometry.IsEqual(num, num2))
							{
								curve = curve2;
								list3.Add(wallDataEx2);
								list2.Add(wallDataEx2);
							}
						}
					}
					WallDataMerge item = new WallDataMerge(curve, list3);
					list.Add(item);
				}
			}
			return list;
		}

		public static Dictionary<GridGroupDataEx, List<WallDataMerge>> GetOutWallDataByGrid(UIApplication uiApp, BaseDataOper baseData)
		{
			Dictionary<GridGroupDataEx, List<WallDataMerge>> dictionary = new Dictionary<GridGroupDataEx, List<WallDataMerge>>();
			try
			{
				List<GridGroupDataEx> gridGroup = baseData.GetGridGroup();
				XYZ center = baseData.GetCenter();
				if (gridGroup.Count == 0 || center == null)
				{
					return dictionary;
				}
				foreach (GridGroupDataEx gridGroupDataEx in gridGroup)
				{
					List<WallDataMerge> list = new List<WallDataMerge>();
					XYZ xyz = gridGroupDataEx.VecGridNum.Normalize();
					Line lineGrid = Line.CreateUnbound(center, xyz);
					Line cv = Line.CreateUnbound(center, Transform.CreateRotation(XYZ.BasisZ, Math.PI*.5).OfVector(xyz));
					List<WallDataMerge> mergeWallData = WallOperattionUtil.GetMergeWallData(uiApp, baseData);
					List<WallDataMerge> curveIntersectWall = WallOperattionUtil.GetCurveIntersectWall(mergeWallData, cv);
					if (curveIntersectWall.Count > 0)
					{
						list.AddRange(curveIntersectWall);
					}
					foreach (WallDataMerge wallDataMerge in mergeWallData)
					{
						if (!WallOperattionUtil.HasWallMergeInLst(uiApp, list, wallDataMerge))
						{
							bool flag = WallOperattionUtil.IsWallInGrid(center, xyz, lineGrid, wallDataMerge.MergeProfileLine);
							bool flag2 = WallOperattionUtil.IsWallIn6MRange(gridGroupDataEx, wallDataMerge.MergeProfileLine, true);
							if (flag && flag2)
							{
								list.Add(wallDataMerge);
							}
						}
					}
					if (list.Count > 0)
					{
						dictionary.Add(gridGroupDataEx, list);
					}
				}
			}
			catch (Exception)
			{
			}
			return dictionary;
		}

		private static bool IsWallInGrid(XYZ ptProfileCenter, XYZ vecGrid, Line lineGrid, Curve cv)
		{
			try
			{
				XYZ endPoint = cv.GetEndPoint(0);
				XYZ endPoint2 = cv.GetEndPoint(1);
				XYZ xyzpoint = lineGrid.Project(endPoint).XYZPoint;
				XYZ xyzpoint2 = lineGrid.Project(endPoint2).XYZPoint;
				XYZ xyz = (xyzpoint - ptProfileCenter).Normalize();
				XYZ xyz2 = (xyzpoint2 - ptProfileCenter).Normalize();
				if (xyz.IsAlmostEqualTo(vecGrid) || xyz2.IsAlmostEqualTo(vecGrid))
				{
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		public static bool IsWallIn6MRange(GridGroupDataEx gridGoup, Curve cv, bool bIsOneContain = true)
		{
			try
			{
				XYZ xyz = null;
				XYZ xyz2 = null;
				ParallelLinesUtils.GetParallelLinesMaxDisEndPoint(gridGoup.LstLine, out xyz, out xyz2, 19.685039370078741, 19.685039370078741);
				if (xyz == null || xyz2 == null)
				{
					return false;
				}
				Line line = Line.CreateBound(xyz, xyz2);
				line.MakeUnbound();
				XYZ xyzpoint = line.Project(cv.GetEndPoint(0)).XYZPoint;
				XYZ xyzpoint2 = line.Project(cv.GetEndPoint(1)).XYZPoint;
				if (bIsOneContain)
				{
					if (Geometry.Is_Point_OnSegment(xyz, xyz2, xyzpoint, 1E-09) || Geometry.Is_Point_OnSegment(xyz, xyz2, xyzpoint2, 1E-09))
					{
						return true;
					}
				}
				else if (Geometry.Is_Point_OnSegment(xyz, xyz2, xyzpoint, 1E-09) && Geometry.Is_Point_OnSegment(xyz, xyz2, xyzpoint2, 1E-09))
				{
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		public static Curve GetOneGridGroupMaxWallCurve(GridGroupDataEx gridGoup, List<WallDataMerge> lstWallMerge)
		{
			Curve result = null;
			try
			{
				List<Curve> list = new List<Curve>();
				XYZ xyz = gridGoup.VecGridNum.Normalize();
				foreach (WallDataMerge wallDataMerge in lstWallMerge)
				{
					Line line = wallDataMerge.MergeProfileLine as Line;
					if (null != line && Geometry.IsVertical(xyz, line.Direction.Normalize()))
					{
						list.Add(line);
					}
				}
				result = ParallelLinesUtils.SortHoriLineWithVector(list, xyz).Last<Curve>().Clone();
			}
			catch (Exception)
			{
				return null;
			}
			return result;
		}

		public static Curve GetWallValidDimCurve(WallDataMerge wall)
		{
			XYZ xyz = null;
			XYZ xyz2 = null;
			try
			{
				WallDataEx wallDataEx = wall.LstWallData.First<WallDataEx>();
				Curve curve = wallDataEx.OutLine.Clone();
				if (null != curve)
				{
					curve.MakeUnbound();
					xyz = curve.Project(wallDataEx.ProfileLine.GetEndPoint(0)).XYZPoint;
					xyz2 = curve.Project(wallDataEx.ProfileLine.GetEndPoint(1)).XYZPoint;
				}
			}
			catch (Exception)
			{
				return null;
			}
			if (xyz != null && xyz2 != null)
			{
				return Line.CreateBound(xyz, xyz2);
			}
			return null;
		}

		public static bool IsWinDoorValid(UIApplication uiApp, List<WallDataMerge> lstWallMerge, Curve cvWinDoor, XYZ vecGrid)
		{
			try
			{
				if (lstWallMerge.Count == 0)
				{
					return true;
				}
				Curve curve = cvWinDoor.Clone();
				curve.MakeUnbound();
				XYZ endPoint = cvWinDoor.GetEndPoint(0);
				XYZ endPoint2 = cvWinDoor.GetEndPoint(1);
				foreach (WallDataMerge wallDataMerge in lstWallMerge)
				{
					XYZ endPoint3 = wallDataMerge.MergeProfileLine.GetEndPoint(0);
					XYZ endPoint4 = wallDataMerge.MergeProfileLine.GetEndPoint(1);
					XYZ xyzpoint = curve.Project(endPoint3).XYZPoint;
					XYZ xyzpoint2 = curve.Project(endPoint4).XYZPoint;
					XYZ xyzpoint3 = curve.Project(endPoint).XYZPoint;
					XYZ xyzpoint4 = curve.Project(endPoint2).XYZPoint;
					if (Geometry.IsEqual(xyzpoint, xyzpoint2, 1E-05))
					{
						if (Geometry.Is_Point_OnSegment(xyzpoint3, xyzpoint4, xyzpoint, 1E-09))
						{
							return false;
						}
					}
					else
					{
						Curve curve2 = Line.CreateBound(xyzpoint, xyzpoint2);
						Curve curve3 = Line.CreateBound(xyzpoint3, xyzpoint4);
                        if (1 != (int)CurveAssistFunc.GetOverlapType(uiApp, curve2, curve3))
						{
							return false;
						}
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static List<WallDataMerge> GetVectorWallMerge(List<WallDataMerge> lstWallMerge, XYZ vecVertGrid)
		{
			List<WallDataMerge> list = new List<WallDataMerge>();
			foreach (WallDataMerge wallDataMerge in lstWallMerge)
			{
				Line line = wallDataMerge.MergeProfileLine as Line;
				if (null != line && Geometry.IsParallel(line.Direction.Normalize(), vecVertGrid))
				{
					list.Add(wallDataMerge);
				}
			}
			return list;
		}

		public static List<WallDataMerge> GetPointVectWallMerge(List<WallDataMerge> lstWallMerge, XYZ ptOrign, XYZ vecGrid)
		{
			List<WallDataMerge> list = new List<WallDataMerge>();
			Line lineGrid = Line.CreateUnbound(ptOrign, vecGrid);
			foreach (WallDataMerge wallDataMerge in lstWallMerge)
			{
				if (WallOperattionUtil.IsWallInGrid(ptOrign, vecGrid, lineGrid, wallDataMerge.MergeProfileLine))
				{
					list.Add(wallDataMerge);
				}
			}
			return list;
		}

		private static bool GetWallDataWidth(Autodesk.Revit.DB.Document doc, WallDataEx wall, out double dWidth)
		{
			dWidth = 0.0;
			try
			{
				Wall wall2 = doc.GetElement(wall.WallId) as Wall;
				if (wall2 != null)
				{
					dWidth = wall2.Width;
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		private static bool IsWallEqual(UIApplication uiApp, WallDataEx wallF, WallDataEx wallS)
		{
			try
			{
                if ((int)CurveAssistFunc.GetOverlapType(uiApp, wallF.ProfileLine, wallS.ProfileLine) == 5)
				{
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		private static bool IsWallMergeEqual(UIApplication uiApp, WallDataMerge wallF, WallDataMerge wallS)
		{
			try
			{
                if ((int)CurveAssistFunc.GetOverlapType(uiApp, wallF.MergeProfileLine, wallS.MergeProfileLine) == 5)
				{
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		private static bool HasWallInLst(UIApplication uiApp, List<WallDataEx> lstWall, WallDataEx wallData)
		{
			try
			{
				foreach (WallDataEx wallS in lstWall)
				{
					if (WallOperattionUtil.IsWallEqual(uiApp, wallData, wallS))
					{
						return true;
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		private static bool HasWallMergeInLst(UIApplication uiApp, List<WallDataMerge> lstWall, WallDataMerge wallData)
		{
			try
			{
				foreach (WallDataMerge wallF in lstWall)
				{
					if (WallOperattionUtil.IsWallMergeEqual(uiApp, wallF, wallData))
					{
						return true;
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		private static List<WallDataMerge> GetCurveIntersectWall(List<WallDataMerge> lstWallData, Curve cv)
		{
			List<WallDataMerge> list = new List<WallDataMerge>();
			try
			{
				foreach (WallDataMerge wallDataMerge in lstWallData)
				{
					if (!(null != wallDataMerge.MergeProfileLine))
					{
						IntersectionResultArray intersectionResultArray = null;
						cv.Intersect(wallDataMerge.MergeProfileLine, out intersectionResultArray);
						if (intersectionResultArray != null && 1 == intersectionResultArray.Size)
						{
							list.Add(wallDataMerge);
						}
					}
				}
			}
			catch (Exception)
			{
			}
			return list;
		}

		private const double m_d6M = 19.685039370078741;

		private const double m_d1MM = 0.0032808398950131233;
	}
}
